package sg.edu.nus.iss.vmcs.ui;

import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import sg.edu.nus.iss.vmcs.controller.ChangeGiver;
import sg.edu.nus.iss.vmcs.controller.TransactionController;
import sg.edu.nus.iss.vmcs.entity.AbstractStoreFactory;
import sg.edu.nus.iss.vmcs.entity.Coin;
import sg.edu.nus.iss.vmcs.entity.StoreItem;
import sg.edu.nus.iss.vmcs.ui.listener.CoinInputListener;
import sg.edu.nus.iss.vmcs.util.Environment;

public class CoinInputBox extends Observable {
	
	private JPanel coinInputBoxPanel = new JPanel();
	private MoneyInsertedPanel mp;
	
	private JLabel invalidLabel;
	private double totalCoinsInserted;
	
	private boolean coinInputEnabled;
	
	public boolean isCoinInputEnabled() {
		return coinInputEnabled;
	}

	private TransactionController txnController;
	
	public void setCoinInputEnabled(boolean coinInputEnabled) {
		this.coinInputEnabled = coinInputEnabled;
		setChanged();
	}

	public CoinInputBox(TransactionController txnController) {
		this.txnController = txnController;
		txnController.setCoinInputBox(this);
		init();
	}
	
	public TransactionController getTxnController() {
		return txnController;
	}

	public void init() {
		mp = new MoneyInsertedPanel(0);
		
		CoinInputListener cil = new CoinInputListener(this);
		StoreItem[] brandStores = AbstractStoreFactory.getInstance(Environment.getMachineModel()).getCashStoreInstance().getItems();
		coinInputBoxPanel.setLayout(new GridLayout(4, 1));
		JPanel jp2 = new JPanel();
		for(int i=0;i<brandStores.length;i++){
			StoreItem item = brandStores[i];
			Coin coin = (Coin) item.getContent();
			
			ObserverButton coin1 = new ObserverButton(coin.getName());
			//int val = (int)coin.getValue();
			coin1.setActionCommand(coin.getId());
			coin1.addActionListener(cil);
			coin1.setEnabled(false);
			addObserver(coin1);
			jp2.add(coin1);
		}
		/*
		ObserverButton coin1 = new ObserverButton("5c");
		ObserverButton coin2 = new ObserverButton("10c");
		ObserverButton coin3 = new ObserverButton("20c");
		ObserverButton coin4 = new ObserverButton("50c");
		ObserverButton coin5 = new ObserverButton("$1");
		ObserverButton coin6 = new ObserverButton("Invalid");
		
		coin1.setActionCommand("5");
		coin2.setActionCommand("10");
		coin3.setActionCommand("20");
		coin4.setActionCommand("50");
		coin5.setActionCommand("1");
		coin6.setActionCommand("-1");
		
				
		coin1.addActionListener(cil);
		coin2.addActionListener(cil);
		coin3.addActionListener(cil);
		coin4.addActionListener(cil);
		coin5.addActionListener(cil);
		coin6.addActionListener(cil);
		
		coin1.setEnabled(false);
		coin2.setEnabled(false);
		coin3.setEnabled(false);
		coin4.setEnabled(false);
		coin5.setEnabled(false);
		coin6.setEnabled(false);
		
		addObserver(coin1);
		addObserver(coin2);
		addObserver(coin3);
		addObserver(coin4);
		addObserver(coin5);
		addObserver(coin6);*/
		
		//JLabel invalidLabel = new JLabel("Invalid Coin");
		invalidLabel = new JLabel("");
		
		
		
		JPanel jp1 = new JPanel();
		jp1.add(new JLabel("Enter coins here"));
		
		//JPanel jp2 = new JPanel();
		//jp2.add(coin1);jp2.add(coin2);jp2.add(coin3);jp2.add(coin4);jp2.add(coin5);jp2.add(coin6);
		
		JPanel jp3 = new JPanel();
		jp3.add(invalidLabel);
		
		
		JPanel jp4 = mp.getMoneyPanel();
		
		JPanel jp5 = NoChangeAvailableDisplay.getInstance().getPanel(); 
		JButton button = new JButton();
		button.setText("Switch change giving strategy");
		button.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				ChangeGiver.currentStrategy = 1 - ChangeGiver.currentStrategy;
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		coinInputBoxPanel.setLayout(new GridLayout(6, 1));
		coinInputBoxPanel.add(jp1);
		coinInputBoxPanel.add(jp2);
		coinInputBoxPanel.add(jp3);
		coinInputBoxPanel.add(jp4);
		coinInputBoxPanel.add(jp5);
		coinInputBoxPanel.add(button);
		
		
		
		
	}

	public void setActive(boolean flag) {
		coinInputBoxPanel.setEnabled(flag);
	}

	/*
	public void update(Observable o, Object arg) {
		if (arg instanceof Boolean) {
			Boolean bool = (Boolean) arg;
			setActive(bool.booleanValue());
		}		
	}	
	*/
	public JPanel getCoinInputBoxPanel() {
		return coinInputBoxPanel;
	}


	public MoneyInsertedPanel getMp() {
		return mp;
	}
	
	
	public void setInvalidLabel(String msg){
		
		this.invalidLabel.setText(msg);
		
	}
	
	
	
	
}